package thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/18 23:46
 */
public class Thread02 {
    static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {

        CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1 ");
            return "part01";

        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2");

            return "part02";

        }, executor);
        /**
         * 两个任务，只要有一个完成，就执行任务3
         * runAfterEitherAsync 不感知结果，自己业务返回值
         * acceptEitherAsync :感知结果，自己没有返回值
         * applyToEitherAsync：能感知结果，并返回结果
         */
        /*CompletableFuture<Void> future03 = future01.runAfterEitherAsync(future02, () -> {
            System.out.println("任务3");
        }, executor);*/

       /* future01.acceptEitherAsync(future02, (res) -> {
            System.out.println("接受到了结果" + res);
        }, executor);*/
        future01.applyToEitherAsync(future02, (res) -> {
            System.out.println("任务三执行");
            return res.toString();
        }, executor);
        System.out.println("main end");
        executor.shutdown();
    }

}

